
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <GLES/gl.h> /* For NDK*/
#include <assert.h>
#include <android/log.h>

#include "app.h"
#include "data.h"



// Total run length is 20 * camera track base unit length (see cams.h).
#define RUN_LENGTH  (20 * CAMTRACK_LEN)


#ifdef __cplusplus
extern "C" {
#endif


static long sStartTick = 0;
static long sTick = 0;

static int  action_idx = 0;

M3D::World *world;
M3D::Camera *camera = NULL;
M3D::Model *model = NULL;
M3D::Light *light = NULL;
//static GLfloat pos[4] = {1.0f, 1.0f, 1.0f, 0.0f};
//static GLfloat pos[4] = {0.0f, 10.0f, 15.0f, 1.0f};
static GLfloat pos[4] = {0.0f, 10.0f, 20.0f, 1.0f};

extern M3D::am_model_t **bodies;
int body_id = 0;

extern M3D::am_model_t **heads;
int head_id = 0;

extern M3D::am_model_t **hair;
int hair_id = 0;

extern M3D::am_model_t **eyebrows_l;
int eyebrow_l_id = 0;

extern M3D::am_model_t **eyebrows_r;
int eyebrow_r_id = 0;

extern M3D::am_model_t **eyes_l;
int eye_l_id = 0;

extern M3D::am_model_t **eyes_r;
int eye_r_id = 0;

extern M3D::am_model_t **noses;
int nose_id = 0;

extern M3D::am_model_t **mouths;
int mouth_id = 0;


M3D::ModelAM *model_body = NULL;
M3D::ModelAM *model_head = NULL;
M3D::ModelAM *model_hair = NULL;
M3D::ModelAM *model_eyebrow_l = NULL;
M3D::ModelAM *model_eyebrow_r = NULL;
M3D::ModelAM *model_eye_l = NULL;
M3D::ModelAM *model_eye_r = NULL;
M3D::ModelAM *model_nose = NULL;
M3D::ModelAM *model_mouth = NULL;

extern bool loadAllMeshes ();
extern void destroyAllMeshes ();
extern void dumpBodies ();

///////////////////////////////////////////////////////////////////////////////



void createDefaultMii ()
{

    model_body = new M3D::ModelAM ();
    model_body->loadModel (bodies[body_id]);

    model_head = new M3D::ModelAM ();
    model_head->loadModel (heads[head_id]);

    model_hair = new M3D::ModelAM ();
    model_hair->loadModel (hair[hair_id]);

    model_eyebrow_l = new M3D::ModelAM ();
    model_eyebrow_l->loadModel (eyebrows_l[eyebrow_l_id]);

    model_eyebrow_r = new M3D::ModelAM ();
    model_eyebrow_r->loadModel (eyebrows_r[eyebrow_r_id]);

    model_eye_l = new M3D::ModelAM ();
    model_eye_l->loadModel (eyes_l[eye_l_id]);

    model_eye_r = new M3D::ModelAM ();
    model_eye_r->loadModel (eyes_r[eye_r_id]);

    model_nose = new M3D::ModelAM ();
    model_nose->loadModel (noses[nose_id]);

    model_mouth = new M3D::ModelAM ();
    model_mouth->loadModel (mouths[mouth_id]);

    printf ("A person is created.\n");


    return;
}

void updateMiiEyebrow ()
{
    if (model_eyebrow_l == NULL)
        model_eyebrow_l = new M3D::ModelAM ();

    model_eyebrow_l->updateModel (eyebrows_l[eyebrow_l_id]);


    if (model_eyebrow_r == NULL)
        model_eyebrow_r = new M3D::ModelAM ();

    model_eyebrow_r->updateModel (eyebrows_r[eyebrow_r_id]);

}

void updateMiiEye ()
{
    if (model_eye_l == NULL)
        model_eye_l = new M3D::ModelAM ();

    model_eye_l->updateModel (eyes_l[eye_l_id]);


    if (model_eye_r == NULL)
        model_eye_r = new M3D::ModelAM ();

    model_eye_r->updateModel (eyes_r[eye_r_id]);

}


void updateMiiNose ()
{
    if (model_nose == NULL)
        model_nose = new M3D::ModelAM ();

    model_nose->updateModel (noses[nose_id]);
}


void updateMiiMouth ()
{
    if (model_mouth == NULL)
        model_mouth = new M3D::ModelAM ();

    model_mouth->updateModel (mouths[mouth_id]);
}



// Called from the app framework.
void appInit(int width, int height)
{
	world = new M3D::World ();
	world->setBgColor (0.0, 255.0, 0.0, 0.0);
	world->setSize (320, 430);
	world->init ();

    camera = new M3D::Camera ();
    camera->setEye (0.0f, 3.0f, 60.0f);

    world->setCamera (camera);

    light = new M3D::Light();
    light->setPosition(pos);
    world->setLight(light);

	loadAllMeshes ();
    createDefaultMii ();

	dumpBodies ();

}


// Called from the app framework.
void appDeinit()
{
    delete world;
    delete camera;
    delete light;

    delete model_body;
    delete model_head;
    delete model_hair;
    delete model_eyebrow_l;
    delete model_eyebrow_r;
    delete model_eye_l;
    delete model_eye_r;
    delete model_nose;
    delete model_mouth;


    destroyAllMeshes ();

}

// Called from the app framework.
/* The tick is current time in milliseconds, width and height
 * are the image dimensions to be rendered.
 */
void appRender(long tick, int width, int height)
{
    if (sStartTick == 0)
        sStartTick = tick;

    // Actual tick value is "blurred" a little bit.
    sTick = (sTick + tick - sStartTick) >> 1;

	world->prepareRender ();

    model_body->renderModel();
    model_head->renderModel();
    model_hair->renderModel();
    model_eyebrow_l->renderModel();
    model_eyebrow_r->renderModel();
    model_eye_l->renderModel();
    model_eye_r->renderModel();
    model_nose->renderModel();
    model_mouth->renderModel();


	world->finishRender ();


}

void appSetModelTextureOffset (float x, float y, float z) {


    //LOGD ("appSetModelTextureOffset: x=%f, y=%f, z=%f\n", x, y, z);


}


void appSetModelRotate (float x, float y, float z) {

	__android_log_print(ANDROID_LOG_INFO, "AndMii", "this is appSetModelRotate.\n");

	model_body->setRotate (x, y, z);
    model_head->setRotate (x, y, z);
    model_hair->setRotate (x, y, z);
    model_eyebrow_l->setRotate (x, y, z);
    model_eyebrow_r->setRotate (x, y, z);
    model_eye_l->setRotate (x, y, z);
    model_eye_r->setRotate (x, y, z);
    model_nose->setRotate (x, y, z);
    model_mouth->setRotate (x, y, z);

}

void appSetModelScale (float x, float y, float z) {

	


}

void appSetModelPosition (float x, float y, float z) {


}
/*
void fontRender (int x, int y, const char *string) {

}
*/


void appSetMiiEyebrow (int id)
{
	eyebrow_l_id = id;
	eyebrow_r_id = id;
	updateMiiEyebrow ();
}

void appSetMiiEye (int id)
{
	eye_l_id = id;
	eye_r_id = id;
	updateMiiEye();
}

void appSetMiiNose (int id)
{
	nose_id = id;
	nose_id = id;
	updateMiiNose();
}

void appSetMiiMouth (int id)
{
	mouth_id = id;
	mouth_id = id;
	updateMiiMouth();
}


#ifdef __cplusplus
}
#endif

